﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UILayoutEditor
{
    public partial class MainForm : Form
    {
        string duneInstallation = @"C:\Users\UserName\Documents\Dune\Working Directory\";
        string graphicsLocation = @"Data\UI_GFX\";
        string uiDataLocation = @"Data\UI_DATA\";

        string caption = "Dune 2000 User Interface Layout Editor";

        ColorList colorList;
        StringTable stringTable;
        List<BaseControl> trackedControls = new List<BaseControl>();

        MouseEventHandler selectDown;// = new System.Windows.Forms.MouseEventHandler(selectedObject_Select);
        MouseEventHandler moveMoved;// = new System.Windows.Forms.MouseEventHandler(selectedObject_MouseMove);
        MouseEventHandler moveDown;//= new System.Windows.Forms.MouseEventHandler(selectedObject_MouseDown);
        MouseEventHandler moveUp;// = new System.Windows.Forms.MouseEventHandler(selectedObject_MouseUp);
        PaintEventHandler generalPaint;
        BaseControl SelectedControl
        {
            get
            {
                return trackedControls[listBox1.SelectedIndex];
            }
        }
        // Used only for tracking previous index of selected control
        int currentTrackedIndex = -1;

        Point centralOffset = new Point(0, 0);
        bool centralDown = false;
        bool blMouse = false;
        bool brMouse = false;
        bool tlMouse = false;
        bool trMouse = false;

        public MainForm()
        {
            InitializeComponent();
            this.Text = caption;
            HideHandles();

            selectDown = new System.Windows.Forms.MouseEventHandler(selectedObject_Select);
            moveMoved = new System.Windows.Forms.MouseEventHandler(selectedObject_MouseMove);
            moveDown = new System.Windows.Forms.MouseEventHandler(selectedObject_MouseDown);
            moveUp = new System.Windows.Forms.MouseEventHandler(selectedObject_MouseUp);
            generalPaint = new PaintEventHandler(general_Paint);
        }
        private void pictureBox1_MouseDown(object sender,
                     System.Windows.Forms.MouseEventArgs e)
        {
            blMouse = true;
        }

        private void pictureBox1_MouseUp(object sender,
                System.Windows.Forms.MouseEventArgs e)
        {
            blMouse = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Load UI colour list
            colorList = ColorList.Load(duneInstallation + uiDataLocation + "colours.uib");

            // Load string table
            stringTable = new StringTable();
            stringTable.Load(duneInstallation + uiDataLocation + "text.uib");
        }

        private void Form1_Click(object sender, EventArgs e)
        {

        }

        void LoadUIL(string filename)
        {
            this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + caption;
            listBox1.Items.Clear();
            propertyGrid1.SelectedObject = null;
            logBox.Clear();
            RemoveTrackedControls();
            HideHandles();

            //System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
            //System.Drawing.Graphics formGraphics = this.CreateGraphics();
            //formGraphics.FillRectangle(myBrush, new Rectangle(0,0,640,400));
            //myBrush.Dispose();
            //formGraphics.Dispose();

            FileStream fs = new FileStream(filename, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            UInt32 entryCount = br.ReadUInt32();

            while (fs.Position < fs.Length)
            {
                ParseControl(br);
            }

            for (int i = 0; i < trackedControls.Count; i++)
            {
                trackedControls[i].BoundControl.MouseDown += selectedObject_Select;
            }

            br.Close();
            fs.Close();
        }
        string ReadLine(BinaryReader br)
        {
            ushort stringLength = br.ReadUInt16();
            char[] charArray = br.ReadChars(stringLength - 1); // Read 1 less so that we don't catch the null terminator
            br.ReadByte(); // Now read and disregard the null terminator
            string line = new String(charArray);
            // Strip out tabs
            line = line.Replace("\t", "");
            int index = line.IndexOf("//"); // Find comments
            if (index != -1)
            {
                // Contains comment so remove
                line = line.Remove(index);
            }
            return line;
        }
        /*
         * BACKGROUND
            TOGGLEBUTTON
            BUTTON
            GROUPBOX
            LISTBOX_MULTI
            LISTBOXEMPTYSLOT
            LISTBOX_CROPPED
            LISTBOX
            CHECKBOX
            MULTISTATEBOX
            EDITBOXFOCUS
            EDITBOX
            TEXTBOX_L_UL
            TEXTBOX_R_UL
            TEXTBOX_UL
            TEXTBOX_L
            TEXTBOX_R
            TEXTBOX
            VALUEBAR_NOVAL
            VALUEBAR
            ANIMATION
            MOVIE
            */
        void ParseControl(BinaryReader br)
        {
            string firstLine = ReadLine(br);
            string controlType = firstLine.Split(' ')[0];
            //listBox1.Items.Add(firstLine);
            switch (controlType.ToUpper())
            {
                case "BACKGROUND":
                    ParseBackground(firstLine.Split(' ')[1], br);
                    listBox1.Items.Add(firstLine);
                    break;
                case "GROUPBOX":
                    ParseGroupBox(firstLine.Split(' ')[1], br);
                    listBox1.Items.Add(firstLine);
                    break;
                //case "TEXTBOX":
                //    ParseTextbox(br);
                //    break;
                //case "TEXTBOX_UL": // Underlined?
                //    ParseTextbox(br);
                //    break;
                case "BUTTON":
                    ParseButton(firstLine.Split(' ')[1], br);
                    listBox1.Items.Add(firstLine);
                    break;
                //case "MOVIE":
                //    ParseMovie(br);
                //    break;
                //case "ANIMATION":
                //    ParseAnimation(br);
                //    break;
                default:
                    // Could not parse, so log out
                    logBox.Text += firstLine + "\r\n";
                    break;

                /*
                 * TODO: 
                 * EDITBOXFOCUS (Private.uil)
                 * TEXTBOX_C - lomis3
                 * TEXTBOX_L - chatops - appears to have some interesting logic, probably applies to other textbox variants too
                 * CHECKBOX - chatops
                 * LISTBOX - see Load
                 */

            }
        }
        Bitmap GetImageForFile(string filename)
        {
            if (filename == "NONE")
            {
                return null;
            }

            string[] files = Directory.GetFiles(duneInstallation + graphicsLocation, filename + ".*");

            if (files.Length == 1)
            {
                string extension = Path.GetExtension(files[0]).ToLower();
                if (extension == ".tga")
                {
                    return Paloma.TargaImage.LoadTargaImage(files[0]);
                }
                else if (extension == ".bmp")
                {
                    return new Bitmap(files[0]);
                }
            }
            else if (files.Length > 1)
            {
                // Pick second, this isn't particularly robust, but will work for Dune 2000 files out the box
                string extension = Path.GetExtension(files[1]).ToLower();
                if (extension == ".tga")
                {
                    return Paloma.TargaImage.LoadTargaImage(files[1]);
                }
                else if (extension == ".bmp")
                {
                    return new Bitmap(files[1]);
                }
            }
            return null;
        }
        void RemoveTrackedControls()
        {
            while (trackedControls.Count > 0)
            {
                splitContainer1.Panel1.Controls.Remove(trackedControls.Last().BoundControl);
                trackedControls.Remove(trackedControls.Last());
            }
        }
        void ParseBackground(string firstArgument, BinaryReader br)
        {
            string controlCloseTag = ReadLine(br);

            UILayoutEditor.Background background = new UILayoutEditor.Background();

            PictureBox newControl = new PictureBox();
            newControl.Image = GetImageForFile(firstArgument);// Paloma.TargaImage.LoadTargaImage(@"C:\Users\UserName\Documents\Dune\Mod1\Data\UI_GFX\WWLOGO.TGA");
            newControl.Size = new Size(640, 400);
            splitContainer1.Panel1.Controls.Add(newControl);
            background.BoundControl = newControl;

            trackedControls.Add(background);
        }
        void ParseGroupBox(string firstArgument, BinaryReader br)
        {
            string name = firstArgument;
            Point location = ParsePoint(ReadLine(br));
            Size size = ParseSize(ReadLine(br));
            string backColor = ReadLine(br);
            string borderColor = ReadLine(br);
            string image = ReadLine(br);
            string controlCloseTag = ReadLine(br);

            // Create D2K control object
            UILayoutEditor.GroupBox groupBox = new UILayoutEditor.GroupBox();
            groupBox.Name = name;
            groupBox.Location = location;
            groupBox.Size = size;

            // Add WF control to form
            PictureBox newControl = new PictureBox();
            newControl.BackColor = colorList.GetColor(backColor);
            newControl.Image = GetImageForFile(image);// Paloma.TargaImage.LoadTargaImage(@"C:\Users\UserName\Documents\Dune\Mod1\Data\UI_GFX\WWLOGO.TGA");
            newControl.Location = location;
            newControl.Size = size;
            newControl.SizeMode = PictureBoxSizeMode.StretchImage;
            splitContainer1.Panel1.Controls.Add(newControl);
            newControl.BringToFront();
            groupBox.BoundControl = newControl;

            trackedControls.Add(groupBox);

        }
        void ParseTextbox(BinaryReader br)
        {
            // See:
            // http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx

            // See:
            // http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesvbcs/thread/a3f2cb62-4473-44d3-ad94-04f69ed52758/
            // http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/02b94a8c-d38f-4d20-8efe-7e74ce12ab13/
            // http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/ab43a6ef-1093-496b-8d59-a39df1cf24d9/

            Point position = ParsePoint(ReadLine(br));
            Point size = ParsePoint(ReadLine(br));
            string backgroundColor = ReadLine(br);
            string hiColor = ReadLine(br);
            string loColor = ReadLine(br);
            string fontColor = ReadLine(br);
            string stringTableKey = ReadLine(br);
            string controlCloseTag = ReadLine(br);

            //System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
            //System.Drawing.Graphics formGraphics = this.CreateGraphics();
            //formGraphics.FillRectangle(myBrush, new Rectangle(position.X, position.Y, size.X, size.Y));
            //myBrush.Dispose();
            //formGraphics.Dispose();
        }
        void ParseAnimation(BinaryReader br)
        {
            Point position = ParsePoint(ReadLine(br));
            Size frameSize = ParseSize(ReadLine(br));
            string textureAndRate = ReadLine(br);
            string something = ReadLine(br);
            string controlCloseTag = ReadLine(br);

            string texture = textureAndRate.Split(' ')[0];
            string rate = textureAndRate.Split(' ')[1];

            Bitmap fullTexture = GetImageForFile(texture);
            Rectangle frameRectangle = new Rectangle(new Point(0, 0), frameSize);
            Bitmap firstFrame = fullTexture.Clone(frameRectangle, fullTexture.PixelFormat);

            PictureBox newControl = new PictureBox();
            newControl.Image = firstFrame;// Paloma.TargaImage.LoadTargaImage(@"C:\Users\UserName\Documents\Dune\Mod1\Data\UI_GFX\WWLOGO.TGA");
            newControl.Location = position;
            newControl.Size = frameSize;
            splitContainer1.Panel1.Controls.Add(newControl);
            newControl.BringToFront();
            //trackedControls.Add(newControl);

        }
        void ParseButton(string firstArgument, BinaryReader br)
        {
            string name = firstArgument;
            Point location = ParsePoint(ReadLine(br));
            Size size = ParseSize(ReadLine(br));
            string stringTableKey = ReadLine(br);
            string highlightColor = ReadLine(br);
            string lowlightColor = ReadLine(br);
            string buttonUpColor = ReadLine(br);
            string buttonDownColor = ReadLine(br);
            string fontUpColor = ReadLine(br);
            string fontDownColor = ReadLine(br);
            string upGfx = ReadLine(br);
            string downGfx = ReadLine(br);
            List<string> actionList = new List<string>();

            string testRead = "";
            while (testRead != "END_BUTTON")
            {
                testRead = ReadLine(br);
                if (testRead != "END_BUTTON")
                {
                    actionList.Add(testRead);
                }
            }

            // Create D2K control object
            UILayoutEditor.Button button = new UILayoutEditor.Button();
            button.Name = name;
            button.Location = location;
            button.Size = size;
            button.StringTextKey = stringTableKey;
            button.FontUpColor = new ColorListEntry(fontUpColor);

            PictureBox pictureBox = new PictureBox();
            pictureBox.BackColor = colorList.GetColor(buttonUpColor);
            pictureBox.Image = GetImageForFile(upGfx);// Paloma.TargaImage.LoadTargaImage(@"C:\Users\UserName\Documents\Dune\Mod1\Data\UI_GFX\WWLOGO.TGA");
            pictureBox.Location = location;
            pictureBox.Size = size;
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            splitContainer1.Panel1.Controls.Add(pictureBox);
            pictureBox.BringToFront();
            button.BoundControl = pictureBox;
            pictureBox.Paint += generalPaint;
            pictureBox.Tag = button;
            trackedControls.Add(button);

            //Label newLabel = new Label();
            //newLabel.Parent = pictureBox;
            //newLabel.BackColor = Color.Transparent;
            //newLabel.Location = position + new Size(size.X / 2 - newLabel.Size.Width / 2, size.Y / 2 - newLabel.Size.Height / 2);
            //newLabel.Text = "Test";
            //newLabel.TextAlign = ContentAlignment.MiddleCenter;
            //this.Controls.Add(newLabel);
            //newLabel.BringToFront();
            //trackedControls.Add(newLabel);
        }
        void ParseMovie(BinaryReader br)
        {
            Point position = ParsePoint(ReadLine(br));
            Point size = ParsePoint(ReadLine(br));
            string operation = ReadLine(br);
            string controlCloseTag = ReadLine(br);
        }
        Point ParsePoint(string point)
        {
            point = point.Replace(",", "");
            string[] split = point.Split(' ');
            return new Point(int.Parse(split[0]), int.Parse(split[1]));
        }
        Size ParseSize(string point)
        {
            point = point.Replace(",", "");
            string[] split = point.Split(' ');
            return new Size(int.Parse(split[0]), int.Parse(split[1]));
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("www.mvinetwork.co.uk/dune");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // TODO: Refactor to use some shared selection method so if a user instead clicks on the control the logic is shared


            // NOTE: The real selected index has already changed by this point
            // So SelectedControl refers to the newly selected control. 
            // currentTrackedIndex is used for tracking previous selection until it is set in this function when it becomes the current index.
            if (currentTrackedIndex != -1)
            {
                trackedControls[currentTrackedIndex].BoundControl.MouseMove -= moveMoved;
                trackedControls[currentTrackedIndex].BoundControl.MouseDown -= moveDown;
                trackedControls[currentTrackedIndex].BoundControl.MouseUp -= moveUp;

                // Change old control to default
                trackedControls[currentTrackedIndex].BoundControl.Cursor = Cursors.Arrow;
            }
 
            currentTrackedIndex = listBox1.SelectedIndex;

            for (int i = 0; i < trackedControls.Count; i++)
            {
                if (i == currentTrackedIndex)
                {
                    trackedControls[i].BoundControl.MouseDown -= selectedObject_Select;
                }
                else
                {
                    trackedControls[i].BoundControl.MouseDown += selectedObject_Select;
                }
            }

            propertyGrid1.SelectedObject = SelectedControl;

            if (SelectedControl is SubSpaceControl)
            {
                // Change new control to size all
                SelectedControl.BoundControl.Cursor = Cursors.SizeAll;
                UpdateHandles();
                SelectedControl.BoundControl.MouseMove += moveMoved;
                SelectedControl.BoundControl.MouseDown += moveDown;
                SelectedControl.BoundControl.MouseUp += moveUp;
            }
            else
            {
                HideHandles();
            }
        }
        // Bottom left
        private void bottomLeft_MouseMove(object sender, MouseEventArgs e)
        {
            if (blMouse)
            {
                if (SelectedControl.BoundControl.Width - e.X > 0 && SelectedControl.BoundControl.Height + e.Y > 0)
                {
                    SelectedControl.BoundControl.Left += e.X;
                    bottomLeft.Left += e.X;
                    SelectedControl.BoundControl.Width -= e.X;

                    SelectedControl.BoundControl.Height += e.Y;// -pictureBox1.Height;
                    bottomLeft.Top += e.Y;// -pictureBox1.Height;
                    topLeft.Left += e.X;
                    bottomRight.Top += e.Y;
                    SelectedControlResized();
                }
            }
        }

        private void bottomLeft_MouseDown(object sender, MouseEventArgs e)
        {
            blMouse = true;
        }

        private void bottomLeft_MouseUp(object sender, MouseEventArgs e)
        {
            blMouse = false;
        }

        private void bottomRight_MouseDown(object sender, MouseEventArgs e)
        {
            brMouse = true;
        }

        private void bottomRight_MouseUp(object sender, MouseEventArgs e)
        {
            brMouse = false;
        }

        private void bottomRight_MouseMove(object sender, MouseEventArgs e)
        {
            if (brMouse)
            {
                if (SelectedControl.BoundControl.Width + e.X > 0 && SelectedControl.BoundControl.Height + e.Y > 0)
                {
                    //this.pictureBox1.Left += e.X;
                    bottomRight.Left += e.X;
                    SelectedControl.BoundControl.Width += e.X;

                    topRight.Left += e.X;
                    bottomLeft.Top += e.Y;

                    SelectedControl.BoundControl.Height += e.Y;// -pictureBox1.Height;
                    bottomRight.Top += e.Y;// -pictureBox1.Height;
                    SelectedControlResized();
                }
            }
        }

        private void topRight_MouseDown(object sender, MouseEventArgs e)
        {
            trMouse = true;
        }

        private void topRight_MouseUp(object sender, MouseEventArgs e)
        {
            trMouse = false;
        }

        private void topRight_MouseMove(object sender, MouseEventArgs e)
        {
            if (trMouse)
            {
                if (SelectedControl.BoundControl.Width + e.X > 0 && SelectedControl.BoundControl.Height - e.Y > 0)
                {
                    //this.pictureBox1.Left += e.X;
                    topRight.Left += e.X;
                    SelectedControl.BoundControl.Width += e.X;
                    SelectedControl.BoundControl.Height -= e.Y;

                    SelectedControl.BoundControl.Top += e.Y;// -pictureBox1.Height;
                    topRight.Top += e.Y;// -pictureBox1.Height;
                    topLeft.Top += e.Y;
                    bottomRight.Left += e.X;
                    SelectedControlResized();
                }
            }
        }

        private void topLeft_MouseDown(object sender, MouseEventArgs e)
        {
            tlMouse = true;
        }

        private void topLeft_MouseUp(object sender, MouseEventArgs e)
        {
            tlMouse = false;
        }

        private void topLeft_MouseMove(object sender, MouseEventArgs e)
        {
            if (tlMouse)
            {
                if (SelectedControl.BoundControl.Width - e.X > 0 && SelectedControl.BoundControl.Height - e.Y > 0)
                {
                    //System.Console.WriteLine(string.Format("eX: {0} eY: {1}  |  Xd: {2} Yd: {3}", e.X, e.Y, pictureBox1.Width - e.X, pictureBox1.Height - e.Y));
                    //this.pictureBox1.Left += e.X;
                    topLeft.Left += e.X;
                    SelectedControl.BoundControl.Left += e.X;
                    SelectedControl.BoundControl.Height -= e.Y;
                    SelectedControl.BoundControl.Width -= e.X;
                    SelectedControl.BoundControl.Top += e.Y;// -pictureBox1.Height;
                    topLeft.Top += e.Y;// -pictureBox1.Height;

                    topRight.Top += e.Y;
                    bottomLeft.Left += e.X;
                    SelectedControlResized();
                }
            }
        }
        
        private void selectedObject_Select(object sender, MouseEventArgs e)
        {
            // HACK
            for (int i = 0; i < trackedControls.Count; i++)
            {
                if (trackedControls[i].BoundControl == sender)
                {
                    listBox1.SelectedIndex = i;
                    return;
                }
            }
        }
        private void selectedObject_MouseUp(object sender, MouseEventArgs e)
        {
            centralDown = false;
        }

        private void selectedObject_MouseDown(object sender, MouseEventArgs e)
        {
            centralDown = true;
            centralOffset = new Point(e.X, e.Y);
        }

        private void selectedObject_MouseMove(object sender, MouseEventArgs e)
        {
            if (centralDown)
            {

                SelectedControl.BoundControl.Left += e.X - centralOffset.X;
                SelectedControl.BoundControl.Top += e.Y - centralOffset.Y;

                SelectedControlResized();
                UpdateHandles();

                //topLeft.Left += e.X;
                //topLeft.Top += e.Y;

                //topRight.Left += e.X;
                //topRight.Top += e.Y;

                //bottomLeft.Left += e.X;
                //bottomLeft.Top += e.Y;

                //bottomRight.Left += e.X;
                //bottomRight.Top += e.Y;
            }
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "UI Layout files (*.uil)|*.uil";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                LoadUIL(dialog.FileName);
            }
        }

        private void rawPreviewButton_Click(object sender, EventArgs e)
        {
            RawPreview rawPreview = new RawPreview();
            rawPreview.Show();
        }


        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            // The control has changed, so update it
            UpdateSelectedControl();
        }
        void SelectedControlResized()
        {
            if (SelectedControl is SubSpaceControl)
            {
                ((SubSpaceControl)SelectedControl).Location = SelectedControl.BoundControl.Location;
                ((SubSpaceControl)SelectedControl).Size = SelectedControl.BoundControl.Size;
                //SelectedControl.BoundControl.Refresh();
                splitContainer1.Panel1.Refresh();
                propertyGrid1.Refresh();
            }
        }
        void UpdateSelectedControl()
        {
            if (SelectedControl is SubSpaceControl)
            {
                SelectedControl.BoundControl.Location = ((SubSpaceControl)SelectedControl).Location;
                SelectedControl.BoundControl.Size = ((SubSpaceControl)SelectedControl).Size;
                UpdateHandles();
            }
        }
        void UpdateHandles()
        {
            topLeft.Visible = true;
            topRight.Visible = true;
            bottomLeft.Visible = true;
            bottomRight.Visible = true;

            Point location = ((SubSpaceControl)SelectedControl).Location;
            Point size = new Point(((SubSpaceControl)SelectedControl).Size);

            topLeft.BringToFront();
            topRight.BringToFront();
            bottomLeft.BringToFront();
            bottomRight.BringToFront();

            topLeft.Location = ((SubSpaceControl)SelectedControl).Location.Add(new Point(-4, -4)); ;
            topRight.Location = ((SubSpaceControl)SelectedControl).Location.Add(new Point(size.X - 4, -4));
            bottomLeft.Location = ((SubSpaceControl)SelectedControl).Location.Add(new Point(-4, size.Y - 4));
            bottomRight.Location = ((SubSpaceControl)SelectedControl).Location.Add(new Point(size.X - 4, size.Y - 4));
        }

        private void topLeft_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            if (e.EscapePressed)
            {
                e.Action = DragAction.Cancel;
            }
        }

        void HideHandles()
        {
            topLeft.Visible = false;
            topRight.Visible = false;
            bottomLeft.Visible = false;
            bottomRight.Visible = false;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.O))
            {
                openButton.PerformClick();
                
                return true;
            }
            if (keyData == (Keys.Control | Keys.S))
            {
                saveButton.PerformClick();

                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void general_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics gr = e.Graphics;

            Font font = new System.Drawing.Font("Verdana", 8, FontStyle.Regular);
            Button button = ((Button)((Control)sender).Tag);
            
            SolidBrush brush = new SolidBrush(colorList.GetColor(button.FontUpColor.Name));

            StringFormat drawFormat = new StringFormat();
            drawFormat.Alignment = StringAlignment.Center;
            drawFormat.LineAlignment = StringAlignment.Center;
            
            string text = stringTable.GetValue(button.StringTextKey);
            RectangleF rect = new RectangleF(0, 0, button.Size.Width, button.Size.Height);
            gr.DrawString(text, font, brush, rect, drawFormat);
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            //// SEE: http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx
            //Graphics gr = e.Graphics;

            //Font font = new System.Drawing.Font("Verdana", 8, FontStyle.Regular);

            //SolidBrush brush = new SolidBrush(Color.Pink);

            //StringFormat drawFormat = new StringFormat();
            //drawFormat.Alignment = StringAlignment.Center;
            //drawFormat.LineAlignment = StringAlignment.Center;

            //string text = "HELLLO";
            //RectangleF rect = new RectangleF(0, 0, 100,100);
            //gr.DrawString(text, font, brush, rect, drawFormat);
        }
    }
}
